package com.nageoffer.shortlink.project.service.impl;

import com.nageoffer.shortlink.project.service.UrlTitleService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 标题接口实现层
 */
@Service
public class UrlTitleServiceImpl implements UrlTitleService {
    /**
     * 根据url获取标题
     *
     * @param url
     * @return
     */
    @Override
    public String getTitleByUrl(String url) {
        try {
            // 连接到 URL 并获取网页的 Document 对象
            Document document = Jsoup.connect(url).get();

            // 获取网页的标题
            return document.title();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "errorGettingTitle";
    }
}
